// pages/index/free/quote_result.js
// import wxCharts from '../../../utils/wxcharts-min.js'
var wxCharts = require('../../../utils/wxcharts-min.js');
//var heads = require("../../../utils/request.js")
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    clicked: -1,
    clickeds: -1,
    clickedse: -1,
    disableds: true,
    start: false,
    showchart: true,
    datatime: '',
    budget: '',
    thestyle: '',
    village: '',
    mode: '',
    alea: '',
    mainroom: '',
    ting: '',
    wei: '',
    yang: '',
    city: '',
    radios: '',
    total: '',
    labour: '',
    matercial: '',
    bid_id: '',
    uuid: '',
    sid: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    let dataArr = options.data.split(',');
    console.log(dataArr)
    
    var id = dataArr[0];
    var alea = dataArr[1];
    var housstyle = dataArr[2]
    var mainroom = dataArr[3]
    var ting = dataArr[4]
    var wei = dataArr[5]
    var yang = dataArr[6]
    var citys = dataArr[7]
    var mobile = dataArr[8]
    var theroom = mainroom + '' + ting + '' + wei + '' + yang
    var bid_id = dataArr[9]
    var uuid = dataArr[10]
    //var sid =dataArr[11]
    console.log('阿达', bid_id)
    console.log('sdas', uuid)
    //console.log('sid为', sid)
    var total = Math.floor((alea / 0.75) * 777)
    var labour = Math.floor(total * 0.44)
    var matercial = Math.floor(total * 0.56)
    console.log(alea)
    if (housstyle == 1) {
      housstyle = '新房装修'
    } else {
      housstyle = '旧房改造'
    }
    setTimeout(function(){
      wx.getStorage({ //获取本地缓存
        key: "sid",
        success: function (res) {
          that.setData({
            sid: res.data
          });
        },
      })
    },1000)
  
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }
    new wxCharts({
      animation: true,
      canvasId: 'pieCanvas',
      type: 'ring',
      extra: {
        ringWidth: 25,
        pie: {
          offsetAngle: -45
        }
      },
      title: {
        name: total,
        color: 'rgb(255,102,0)',
        fontSize: 20
      },
      subtitle: {
        name: '总价',
        color: 'rgb(102,102,102)',
        fontSize: 15
      },
      series: [{
        name: '人工费',
        data: labour,
        color: 'rgb(252,149,149)',
        stroke: false
      }, {
        name: '材料费',
        data: matercial,
        color: 'rgb(146,197,255)',
        stroke: false
      }, {
        name: '设计费',
        data: 0,
        color: 'rgb(157,223,146)',
        stroke: false
      }, {
        name: '质检费',
        data: 0,
        color: 'rgb(255,170,108)',
        stroke: false
      }],
      disablePieStroke: true,
      width: windowWidth,
      height: 200,
      dataLabel: false,
      legend: false,
      padding: 0
    })
    this.setData({
      city: citys,
      alea: alea,
      houstyle: theroom,
      radios: housstyle,
      total: total,
      labour: labour,
      matercial: matercial,
      bid_id: bid_id,
      uuid: uuid
    })
    console.log(options)
  },
  showmodel: function(e) {
    var that = this
    that.get_material_unitprice()
    wx.canvasToTempFilePath({
      canvasId: 'pieCanvas',
      success: function success(res) {
        wx.saveFile({
          tempFilePath: res.tempFilePath,
          success: function success(res) {
            console.log('saved::' + res.savedFilePath);
            var imgpath = res.savedFilePath
            that.setData({
              showimg: imgpath
            })
          },
          complete: function fail(e) {
            console.log(e.errMsg);
          }
        });
      },
      complete: function complete(e) {
        console.log(e.errMsg);
      }
    });
    this.setData({
      model_mask: true,
      model_content: true,
      showchart: false,
      saveimg: true
    })
  },
  get_material_unitprice: function(e) {
    var that = this
    var area = that.data.alea
    var sid = this.data.sid
    wx.request({
      url: app.apiUrl('c=zhaobiao/index&a=get_estimate_improve_info'),
      //url: ('http://www.newx.com/api/weixinapp/?c=zhaobiao/index&a=get_estimate_improve_info'),
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'PHPSESSID=' + sid
      },
      method: "GET",
      success: function(res) {
        console.log('请求成功', res.data)
        var polist = res.data.datas
        that.setData({
          postlist: polist
        })
      },
      fail: function(res) {
        console.log('请求失败', res.data)
      }
    })
  },
  closemask: function(e) {
    this.setData({
      showchart: true,
      model_mask: false,
      model_content: false,
      saveimg: true
    })
  },
  //房子装修周期选择
  chose_data: function(e) {
    var that = this;
    var index = parseInt(e.currentTarget.id);
    //var value = that.data.choosedata[0].days[index];
    var time = e.target.dataset.time
    console.log(e)
    if (this.data.clicked == e.currentTarget.id) {
      this.setData({
        clickId: +1,
      })
      return;
    }
    that.setData({
      clicked: e.currentTarget.id,
      datatime: time
    })
    console.log(index)
  },
  //房子预算选择
  chose_room: function(e) {
    var that = this;
    var index = parseInt(e.currentTarget.id);
    //var value = that.data.choosedata[1].prices[index];
    var yusu = e.target.dataset.money
    if (this.data.clickeds == e.currentTarget.id) {
      this.setData({
        clickId: +1,
      })
      return;
    }
    that.setData({
      clickeds: e.currentTarget.id,
      budget: yusu,
    })
    // console.log(value)
  },
  //房子装修方式选择
  chose_mode: function(e) {
    var that = this;
    var index = parseInt(e.currentTarget.id);
    //var value = that.data.choosedata[2].themode[index];
    var style = e.target.dataset.style
    if (this.data.clickedse == e.currentTarget.id) {
      this.setData({
        clickId: +1,
      })
      return;
    }
    that.setData({
      clickedse: e.currentTarget.id,
      //mode: value,
      thestyle: style
    })
    // console.log(value)
  },

  //按钮输入
  bindKeyInput: function(e) {
    var value = e.detail.value
    this.setData({
      modelclear: true,
      disableds: false,
      start: true,
      village: value
    })
    //console.log(value)
  },
  clearboth: function(e) {
    this.setData({
      quarters: '',
      modelclear: false,
      disableds: true,
      start: false,
    })
  },
  settime: function () {
    var that = this
    setTimeout(function () {
      that.setData({
        alertmess: false
      })
    }, 2000)
  },
 //点击其他地方隐藏
  mask:function(){
    this.setData({
      model_mask: false,
      model_content: false,
      showchart: true,
      saveimg: false
    })
  },
  //form表单提交
  formSubmit: function(e) {
    var that = this
    var city = that.data.city
    //var mode = this.data.mode
    var datatime = that.data.datatime
    var value = e.detail.value
    var alea = that.data.alea
    var total = that.data.total
    var labour = that.data.labour
    var matercial = that.data.matercial
    var housty = that.data.houstyle
    var yagshi = that.data.radios
    var time = that.data.datatime
    var budget = that.data.budget
    var style = that.data.thestyle
    var village = that.data.village
    var sid = that.data.sid
    var bid_id = that.data.bid_id
    var uuid = that.data.uuid
    console.log(sid)
    this.setData({
      value: value,
      //mode: mode,
      datatime: datatime,
      budget: budget,
    })
    if (time == '') {
      that.setData({
        alertmess: true,
        message: '请选择房屋装修周期！'
      })
      that.settime()
    } else if (budget == '') {
      that.setData({
        alertmess: true,
        message: '请选择房屋装修预算！'
      })
      that.settime()
    } else if (style == '') {
      that.setData({
        alertmess: true,
        message: '请选择房屋装修方式！'
      })
      that.settime()
    } else if (village == '') {
      that.setData({
        alertmess: true,
        message: '请输入小区的名字！'
      })
      that.settime()
    } else {
      wx.request({
        url: app.apiUrl('c=zhaobiao/index&a=update_estimate_info	'),
        //url: ('http://www.newx.com/api/weixinapp/?c=zhaobiao/index&a=update_estimate_info'),
        data: {
          decoration_cycle: time,
          decoration_way: style,
          decoration_budget: budget,
          address: village,
          bid_id: bid_id,
          uuid: uuid
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': 'PHPSESSID=' + sid
        },
        method: "GET",
        success: function(res) {
          console.log('新的bid_id是', res.data)  
          var bid_id =res.data.datas.bid_id
          //console.log('这个是',bid_id)
          var uuid =res.data.datas.uuid
          var code = res.data.code
          var message = res.data.error
          if (code == 200) {
            wx: wx.navigateTo({
              url: 'final_result?data=' + [city, alea, total, labour, matercial, housty, yagshi,bid_id,uuid]
            })
            that.setData({
              model_mask: false,
              model_content: false,
              showchart: true,
              saveimg: false
            })
          }
          else if(code ==400){
            that.setData({
              alertmess: true,
              message: message
            })
            that.settime()
          }
        },
        fail: function(res) {
          console.log('请求失败', res.data)
        }
      })
    }
    console.log(city)
  },
})